﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using OPMS_V1.DOL;

namespace OPMS_V1.BOL
{
    /// <summary>
    /// This class is used for managing the property List.
    /// From here we can edit, delete the Property as we want.
    /// </summary>
    public class ManageProperties
    {
        //Initializing the DOL Class associated with it.
        S_PDataContext dc = new S_PDataContext();
        ListProperties list = new ListProperties();
        DataSet ds = new DataSet();


        /// <summary>
        /// This function is used for returning the datasource for the Grid View
        /// When the user presses the link to change the Page of the Grid View this Function will be executed.
        /// It return the records the then is used to bind the grid view with the new records.
        /// (Complete Properties)
        /// </summary>
        /// <returns></returns>
        public DataSet GetDataSource()
        {
            //Getting the dataset from the DOL ListProperties
            DataSet ds = list.GetDataSource();
            //Returning the dataset to the main calling class
            return ds;
        }

        /// <summary>
        /// This function is used for returning the datasource for the Grid View
        /// When the user presses the link to change the Page of the Grid View this Function will be executed.
        /// It return the records the then is used to bind the grid view with the new records.
        /// (Incomplete Properties)
        /// </summary>
        /// <returns></returns>
        public DataSet GetIncompleteDataSource()
        {
            //Getting the dataset from the DOL ListProperties
            DataSet ds = list.GetIncompleteDataSource();
            //Returning the dataset to the main calling class
            return ds;
        }

        /// <summary>
        /// This function is used for returning the datasource for the Grid View
        /// It return the completed property Lists and then binds to the Grid View
        /// </summary>
        /// <returns></returns>
        public DataSet GetCompletedPropertyList()
        {
            //Getting the dataset from the DOL ListProperties
            DataSet ds = list.GetCompletedPropertyList();
            //Returning the dataset to the main calling class
            return ds;
        }

        /// <summary>
        /// This function is used for returning the datasource for the Grid View
        /// It return the completed property Lists and then binds to the Grid View
        /// </summary>
        /// <returns></returns>
        public DataSet GetIncompletedProperties()
        {
            //Getting the dataset from the DOL ListProperties
            DataSet ds = list.GetIncompletedProperties();
            //Returning the dataset to the main calling class
            return ds;
        }

        /// <summary>
        /// This function is used for returning the datasource for the Grid View
        /// It return the completed property Lists and then binds to the Grid View
        /// </summary>
        /// <returns></returns>
        public DataSet ListCompletedPropertyList()
        {
            //Getting the dataset from the DOL ListProperties
            DataSet ds = list.ListCompletedPropertyList();
            //Returning the dataset to the main calling class
            return ds;
        }

        /// <summary>
        /// This function is used for returning the datasource for the Grid View
        /// It return the completed property Lists and then binds to the Grid View
        /// </summary>
        /// <returns></returns>
        public DataSet ListIncompletedProperties()
        {
            //Getting the dataset from the DOL ListProperties
            DataSet ds = list.ListIncompletedProperties();
            //Returning the dataset to the main calling class
            return ds;
        }

        /// <summary>
        /// This function is used for Getting all the details of Home Property
        /// It gets the single result of the selected property
        /// </summary>
        /// <param name="propID"></param>
        /// <returns></returns>
        public DataSet GetEditHomeDetails(string propID)
        {
            //Getting the dataset from the DOL ListProperties
            DataSet ds = list.GetEditHomeDetails(propID);
            //Returning the dataset to the main calling class
            return ds;
        }

        /// <summary>
        /// This function is used for Getting all the details of Land Property
        /// It gets the single result of the selected property
        /// </summary>
        /// <param name="propID"></param>
        /// <returns></returns>
        public DataSet GetEditLandDetails(string propID)
        {
            //Getting the dataset from the DOL ListProperties
            DataSet ds = list.GetEditLandDetails(propID);
            //Returning the dataset to the main calling class
            return ds;
        }

        /// <summary>
        /// This function is used for getting the Area, Postal Code and State with the given Area
        /// </summary>
        /// <param name="AreaID"></param>
        /// <returns></returns>
        public DataSet GetArea(string AreaID)
        {
            //Getting the dataset from the DOL ListProperties
            DataSet ds = list.GetArea(AreaID);
            //Returning the dataset to the main calling class
            return ds;
        }


        /// <summary>
        /// This function is used for getting the image paths for the Property Ad
        /// </summary>
        /// <param name="PropID"></param>
        /// <returns></returns>
        public DataSet GetImages(string PropID)
        {
            //Getting the dataset from the DOL ListProperties
            DataSet ds = list.GetImages(PropID);
            //Returning the dataset to the main calling class
            return ds;
        }


        /// <summary>
        /// This function is used for creating sessions
        /// This functiuon is executed when the user preeses the Edit Button on the Property List
        /// </summary>
        /// <param name="ds1"></param>
        #region Create Sessions on Edit
        public void createSessions(DataSet ds)
        {
            //Creating the DataSet for Area
            DataSet area = new DataSet();

            //Getting the session for Property ID
            HttpContext.Current.Session["propertyID"] = ds.Tables[0].Rows[0][0].ToString();
            //Getting the session for LandLord ID
            HttpContext.Current.Session["landlordID"] = ds.Tables[0].Rows[0][1].ToString();
            //Getting the session for Property Name
            HttpContext.Current.Session["PropertyName"] = ds.Tables[0].Rows[0][2].ToString();
            //Getting the session for Property No.
            HttpContext.Current.Session["PropertyNo"] = ds.Tables[0].Rows[0][3].ToString();
            //Getting the session for StreetNo
            HttpContext.Current.Session["StreetNo"] = ds.Tables[0].Rows[0][4].ToString();
            //Getting the session for StreetName
            HttpContext.Current.Session["StreetName"] = ds.Tables[0].Rows[0][5].ToString();
            //Getting the session for Area ID
            HttpContext.Current.Session["AreaID"] = ds.Tables[0].Rows[0][6].ToString();

            string areaID = HttpContext.Current.Session["AreaID"].ToString();
            area = GetArea(areaID);
            HttpContext.Current.Session["Area"] = area.Tables[0].Rows[0][0].ToString();
            HttpContext.Current.Session["PostCode"] = area.Tables[0].Rows[0][1].ToString();
            HttpContext.Current.Session["State"] = area.Tables[0].Rows[0][2].ToString();

            //Creating session for Fixed Price
            try
            {
                string FixedPrice = ds.Tables[0].Rows[0][7].ToString();
                if (FixedPrice != null || FixedPrice != "")
                {
                    HttpContext.Current.Session["FixedPrice"] = FixedPrice;
                }
                else
                {
                    HttpContext.Current.Session["FixedPrice"] = "";
                }
            }
            catch (Exception)
            { HttpContext.Current.Session["FixedPrice"] = ""; }

            //Creating session for PriceRangeFrom
            try
            {
                string PriceRangeFrom = ds.Tables[0].Rows[0][8].ToString();
                if (PriceRangeFrom != null || PriceRangeFrom != "")
                {
                    HttpContext.Current.Session["PriceRangeFrom"] = PriceRangeFrom;
                }
                else
                {
                    HttpContext.Current.Session["PriceRangeFrom"] = "";
                }
            }
            catch (Exception)
            { HttpContext.Current.Session["PriceRangeFrom"] = ""; }

            //Creating session for PriceRangeTo
            try
            {
                string PriceRangeTo = ds.Tables[0].Rows[0][9].ToString();
                if (PriceRangeTo != null || PriceRangeTo != "")
                {
                    HttpContext.Current.Session["PriceRangeTo"] = PriceRangeTo;
                }
                else
                {
                    HttpContext.Current.Session["PriceRangeTo"] = "";
                }
            }
            catch (Exception)
            { HttpContext.Current.Session["PriceRangeTo"] = ""; }

            //Creating session for Bond
            try
            {
                string Bond = ds.Tables[0].Rows[0][10].ToString();
                if (Bond != null || Bond != "")
                {
                    HttpContext.Current.Session["Bond"] = Bond;
                }
                else
                {
                    HttpContext.Current.Session["Bond"] = "";
                }
            }
            catch (Exception)
            { HttpContext.Current.Session["Bond"] = ""; }

            //Getting the session for Property Type
            HttpContext.Current.Session["PropertyType"] = ds.Tables[0].Rows[0][11].ToString();
        }
        #endregion Create Sessions on Edit


        /// <summary>
        /// This function is used to create the property Ad HttpContext.Current.Session in 
        /// order to get the Selected Mode for Placing an AD
        /// </summary>
        # region Create Property Ad session
        public void propertyAd()
        {
            //Getting the Property Status
            string propStatus = HttpContext.Current.Session["PropertyStatus"].ToString();

            switch (propStatus)
            {
                //If the status is set as Share
                case "Share":
                    {
                        HttpContext.Current.Session["propertyAdType"] = "ForShare";
                        break;
                    }
                //If the status is set as Rent
                case "Rent":
                    {
                        HttpContext.Current.Session["propertyAdType"] = "ForRent";
                        break;
                    }
                //If the status is set as Sale
                case "Sale":
                    {
                        HttpContext.Current.Session["propertyAdType"] = "ForSell";
                        break;
                    }
            }
        }
        # endregion Create Property Ad session


        /// <summary>
        /// This function is called by the Grid View Edit Properties.
        /// It is used for setting the HttpContext.Current.Session in order to let the user to edit the properties
        /// </summary>
        /// <param name="propID"></param>
        /// <param name="propType"></param>
        public void propertyEdit(string propID, string propType)
        {
            Boolean condition = false;
            string recordsExists;
            DataSet images = new DataSet();

            //If the property type is anything other than Land
            if (!propType.Equals("Land"))
            {
                //Get all the details for Home Details
                ds = GetEditHomeDetails(propID);

                //Checking for existance of the records
                try
                {
                    recordsExists = ds.Tables[0].Rows[0][0].ToString();
                }
                catch (Exception)
                {
                    recordsExists = null;
                }

                if (recordsExists != null)
                {
                    try
                    {
                        //Calling the function to create the sessions
                        createSessions(ds);
                        //This function is used for getting images for the Property
                        images = GetImages(propID);

                        //Creating sessions for Images
                        //As the database consists of 5 images
                        HttpContext.Current.Session["Image1"] = images.Tables[0].Rows[0][0].ToString();
                        HttpContext.Current.Session["Image2"] = images.Tables[0].Rows[0][1].ToString();
                        HttpContext.Current.Session["Image3"] = images.Tables[0].Rows[0][2].ToString();
                        HttpContext.Current.Session["Image4"] = images.Tables[0].Rows[0][3].ToString();
                        HttpContext.Current.Session["Image5"] = images.Tables[0].Rows[0][4].ToString();

                        //Creating the session for Bedroom
                        HttpContext.Current.Session["BedroomNo"] = ds.Tables[0].Rows[0][12].ToString();
                        //Creating the session for Bathroom
                        HttpContext.Current.Session["BathroomNo"] = ds.Tables[0].Rows[0][13].ToString();
                        //Creating the session for FloorNo
                        HttpContext.Current.Session["FloorNo"] = ds.Tables[0].Rows[0][14].ToString();
                        //Creating the session for HouseSize
                        HttpContext.Current.Session["HouseSize"] = ds.Tables[0].Rows[0][15].ToString();

                        //Creating the session for YearBuilt
                        string yearBuilt = ds.Tables[0].Rows[0][16].ToString();
                        if (yearBuilt == "0")
                        {
                            HttpContext.Current.Session["YearBuilt"] = "";
                        }
                        else
                        {
                            HttpContext.Current.Session["YearBuilt"] = yearBuilt;
                        }
                        //Creating the session for Property Status
                        HttpContext.Current.Session["PropertyStatus"] = ds.Tables[0].Rows[0][17].ToString();

                        //This function sets the Property Ad Type of what the user had placed ad in
                        propertyAd();

                        //Creating the session for Dwelling
                        HttpContext.Current.Session["Dwellings"] = ds.Tables[0].Rows[0][18].ToString();
                        //Creating the session for Heading
                        HttpContext.Current.Session["Heading"] = ds.Tables[0].Rows[0][19].ToString();
                        //Creating the session for Description
                        HttpContext.Current.Session["Description"] = ds.Tables[0].Rows[0][20].ToString();
                        //Creating the session for FirstName
                        HttpContext.Current.Session["FirstName"] = ds.Tables[0].Rows[0][21].ToString();
                        //Creating the session for LastName
                        HttpContext.Current.Session["LastName"] = ds.Tables[0].Rows[0][22].ToString();
                        //Creating the session for ContactMethod
                        HttpContext.Current.Session["ContactMethod"] = ds.Tables[0].Rows[0][23].ToString();
                        //Creating the session for Email
                        HttpContext.Current.Session["Email"] = ds.Tables[0].Rows[0][24].ToString();
                        //Creating the session for Phone
                        HttpContext.Current.Session["Phone"] = ds.Tables[0].Rows[0][25].ToString();
                        condition = true;
                    }
                    catch (Exception)
                    { condition = false; }
                }
            }
            else
            {
                //Get all the details for Home Details
                ds = GetEditLandDetails(propID);

                //Checking for existance of the records
                try
                {
                    recordsExists = ds.Tables[0].Rows[0][0].ToString();
                }
                catch (Exception)
                {
                    recordsExists = null;
                }

                if (recordsExists != null)
                {
                    //Calling the function to create the sessions
                    createSessions(ds);
                    HttpContext.Current.Session["NumberHomes"] = ds.Tables[0].Rows[0][12].ToString();
                    HttpContext.Current.Session["Frontage"] = ds.Tables[0].Rows[0][13].ToString();
                    HttpContext.Current.Session["Depth"] = ds.Tables[0].Rows[0][14].ToString();
                    HttpContext.Current.Session["StreetFacing"] = ds.Tables[0].Rows[0][15].ToString();

                    try
                    {
                        string streetwidth = ds.Tables[0].Rows[0][16].ToString();
                        if (streetwidth != null || streetwidth != "")
                        {
                            HttpContext.Current.Session["StreetWidth"] = streetwidth;
                        }
                        else
                        {
                            HttpContext.Current.Session["StreetWidth"] = "";
                        }
                    }
                    catch (Exception)
                    { HttpContext.Current.Session["StreetWidth"] = ""; }

                    //Creating the session for Property Status
                    HttpContext.Current.Session["PropertyStatus"] = ds.Tables[0].Rows[0][17].ToString();

                    //This function sets the Property Ad Type of what the user had placed ad in
                    propertyAd();

                    string sides = ds.Tables[0].Rows[0][18].ToString();

                    if (sides == "4")
                    {
                        HttpContext.Current.Session["Standard"] = 4;
                    }
                    else
                    {
                        HttpContext.Current.Session["NonStandard"] = sides;
                    }
                    //Creating the session for Dwelling
                    HttpContext.Current.Session["Dwellings"] = ds.Tables[0].Rows[0][19].ToString();
                    //Creating the session for Heading
                    HttpContext.Current.Session["Heading"] = ds.Tables[0].Rows[0][20].ToString();
                    //Creating the session for Description
                    HttpContext.Current.Session["Description"] = ds.Tables[0].Rows[0][21].ToString();
                    //Creating the session for FirstName
                    HttpContext.Current.Session["FirstName"] = ds.Tables[0].Rows[0][22].ToString();
                    //Creating the session for LastName
                    HttpContext.Current.Session["LastName"] = ds.Tables[0].Rows[0][23].ToString();
                    //Creating the session for ContactMethod
                    HttpContext.Current.Session["ContactMethod"] = ds.Tables[0].Rows[0][24].ToString();
                    //Creating the session for Email
                    HttpContext.Current.Session["Email"] = ds.Tables[0].Rows[0][25].ToString();
                    //Creating the session for Phone
                    HttpContext.Current.Session["Phone"] = ds.Tables[0].Rows[0][26].ToString();
                }
            }
        }
    }
}
